package com.lp.chapter001.controller.easypoi;

import cn.afterturn.easypoi.csv.CsvExportUtil;
import cn.afterturn.easypoi.csv.entity.CsvExportParams;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.google.common.collect.Lists;
import com.lp.chapter001.excel.easypoi.EasypoiExcel;
import com.lp.chapter001.exception.BizException;
import com.lp.chapter001.handler.easypoi.EasypoiStudentExportHandler;
import com.lp.chapter001.service.easypoi.EasypoiService;
import com.lp.chapter001.util.ResultData;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Easypoi-Controller
 * @author LP to 2024/02/27
 */
@RestController
@RequestMapping("/easypoi")
@AllArgsConstructor
@Slf4j
public class EasypoiController {

    private final EasypoiService easypoiService;
    private final EasypoiStudentExportHandler easypoiStudentExportHandler;

    /**
     * 下载导入模版
     * @author LP to 2024/2/27
     */
    @RequestMapping("/download")
    public void easypoiDownload(HttpServletResponse response) {
        ExportParams params = new ExportParams();
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            Workbook workbook = ExcelExportUtil.exportExcel(params, EasypoiExcel.class, Lists.newArrayList());
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception e) {
            throw new BizException("下载easypoi导入模板失败");
        }
    }

    /**
     * 导入
     * @author LP to 2024/2/27
     */
    @RequestMapping("/import")
    public ResultData<?> easypoiImport(MultipartFile file) {
        if (file.isEmpty()) {
            throw new BizException("导入文件为空");
        }
        easypoiService.easypoiImport(file);
        return ResultData.success();
    }

    /**
     * 导出
     * @author LP to 2024/2/27
     */
    @PostMapping("/export")
    public void easypoiExport(HttpServletResponse response) {
        try {
            long startTime = System.currentTimeMillis();
            response.setContentType("text/csv");
            ServletOutputStream outputStream = response.getOutputStream();
            CsvExportParams csvExportParams = new CsvExportParams();
            CsvExportUtil.exportCsv(csvExportParams, EasypoiExcel.class, easypoiStudentExportHandler, null, outputStream);
            outputStream.flush();
            outputStream.close();
            log.info("导出耗时/ms:" + (System.currentTimeMillis() - startTime));
        } catch (Exception e) {
            log.error("easypoi导出失败,e:{}", e.getMessage(), e);
        }
    }
}
